
<?php
class User_IndexController extends Zend_Controller_Action
{
    public function init()
    {
		// $this->_helper->layout()->setLayout('user_index');
        /* Initialize action controller here */
		$this->_SESSION=new Zend_Session_Namespace();
    }

    public function indexAction()
    {
		if(!isset($this->_SESSION->iduser))
			header("location: http://localhost/hoidap/Source/user/login");
			
		$this->view->iduser = $this->_SESSION->iduser;
		$this->view->firstname = $this->_SESSION->firstname;
		$this->view->lastname = $this->_SESSION->lastname;
		
		$user = new User_Model_DbTable_Users();
		$grfs = new Friendship_Model_Dbtable_RequestFriendship();
		
	//Lay request friendships
    	//$array_fr = $grfs ->getrequestfriendships(1);
    	//$this->view->array_id_fr = $array_fr;

		
	//Lay ds ban be ngau nhien theo ban chung (lay lan dau tien khi load trang)
		$friend = $user-> getuser_friends(1);//Thay iduser
		$this->view->ar_friend = $friend;
	
		/* TODO: gan Id */
		$id=1;
		$like_user =1;
		$post = new Posts_Model_DbTable_Posts();
        $this->view->post = $post->getListPostsByUser($id, $like_user);	
		
		
		
		$limit = 5;
		$start =0;
		$parent =0;
		$catego = new Categories_Model_DbTable_Categories();
		$this->view->Listscate = $catego->listAllCat($limit, $start, $parent);	
		
		/*
		//chuyen ngay thang
		$model_users = new User_Model_DbTable_Users();
		$this->view->date = $model_users->convertdate("dddee");	
		*/
	
        // action body
		
		//TODO: 
		/*
		$e = microtime(true);
		echo $e - $s;
		*/
    }
	
	public function registerAction()
	{
	
	}
	
	public function testAction()
	{
		$this->_helper->viewRenderer->setNoRender(true);
        $this->_helper->layout->disableLayout();
		echo 123;
	}
	//Action lay 1 mang chua 1 friend de ajax xu ly(main.js)
	public function get1friendrandAction()
	{
		//tat layout
		$this->_helper->viewRenderer->setNoRender(true);
        $this->_helper->layout->disableLayout();
		
		$user = new User_Model_DbTable_Users();
		$vitri = $_POST['vitri'];
		$str_id_friend_old = $_POST['str_id_friend'];

		$ar_id_friend = explode(",",$str_id_friend_old);//Chuyen tu chuoi id (1,2,3,4,5) thanh mang
		$count = count($ar_id_friend);
		$d=0;
		while($d<1){
			$flag = 0;
			$friend = $user-> get1user_friends_rand(1);
			for($i=0; $i<$count; $i++)
			{
				if($ar_id_friend[$i]==$friend['id'])// KT xem id lay duoc co nam trong ds id hien thi ngoai trang chu ko
				{
					$flag=1;break;
				}
			}
			if($flag == 0) $d++;
		}
		
		$ar_id_friend[$vitri-1]=$friend['id'];//Thay id tai vi tri do bang gia tri khac
		$str_id_friend_new = "";
		for($i=0; $i<$count; $i++){
			$i>0 ? $str_id_friend_new .=','.$ar_id_friend[$i] : $str_id_friend_new .=$ar_id_friend[$i];// chuyen mang thanh chuoi lai
		}
		//print_r($ar_id_friend);
		//echo $str_id_friend_new;
		
		//$friend = $user-> get1user_friends_rand(1);
		$kq=array('id'=>$friend['id'],'user_firstname'=>$friend['user_firstname'],'user_lastname'=>$friend['user_lastname'],'banchung'=>$friend['banchung'],'user_avata'=>$friend['user_avata'],'str_id_friend'=>"$str_id_friend_new");
		echo Zend_Json::encode($kq);
	}


}

